package org.threebitsoftware.tbb.dao.bridge;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ListIterator;

import javax.swing.text.html.HTML.Tag;

import org.apache.log4j.Logger;
import org.threebitsoftware.dao.generated.ContactTag;
import org.threebitsoftware.dao.generated.ContactTagKey;
import org.threebitsoftware.dao.generated.dao.ContactTagDAO;
import org.threebitsoftware.dao.generated.orm.ContactTagDAOImpl;
import org.threebitsoftware.tbb.dao.DaoHelper;
import org.threebitsoftware.tbb.dao.EmailAddress;
import org.threebitsoftware.tbb.dao.PhoneNumber;
import org.threebitsoftware.tbb.dao.TagForContact;

import com.pew.util.dao.DatabaseUtil;

/*
 * Created: Nov 25, 2009
 * Original Author: Elwin
 * Project:
 * Copyright:
 * 
 * Purpose:
 */

/*
 * Copyright (c) 2009, 3BitSoftware
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the <organization> nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public class TagBridge {
	private static Logger cLogger = Logger.getLogger(TagBridge.class);

		
	public static void storeTag(TagForContact myDomainObject)
	{
		ContactTagDAO lDAO = new ContactTagDAOImpl();
		
		if(myDomainObject != null)
		{
			if(myDomainObject.isMarkedForDeletion())
			{
				try {
					lDAO.delete(myDomainObject.getKeyObject(), DaoHelper.getDBConnection());
				}
				catch(SQLException sqlex)
				{
					cLogger.warn("Could not delete:" + myDomainObject.toString());
				}
			}
			else
			{
				// Create; these are never updated or deleted.
				ContactTag lDaoObject = mapDomainToDao(myDomainObject);
				
				try {
					lDAO.create(lDaoObject, DaoHelper.getDBConnection());
				}
				catch(SQLException sqlex)
				{
					cLogger.warn("Tried to create a tag which may allready exist: SQL State: " + sqlex.getSQLState(), sqlex);
				}
			}
		}
		else
		{
			cLogger.warn("Attempted to store a null object; nothing done.");
		}
		
	}

	/**
	 * Stores multiple objects into the storage medium.
	 * @param myContact ; An array of objects.
	 */
	public static void storeContact(TagForContact myObject[])
	{
		cLogger.debug("Entered storeContact(myContact[])");
		
		int liSize = myObject.length;
		for(int i = 0; i < liSize; i++) {
			storeTag(myObject[i]);
		}
		
		cLogger.info("Worked on storing " + liSize + " phone numbers.");
		
	}
	
	public static TagForContact[] getTags(String myQuery)
	{
		TagForContact lContactTag[] = null;
		
		String lStrQuery = 
			"SELECT ID_CONTACT, S_TAG FROM CONTACT_TAG WHERE "
			+ myQuery;
		
		try {
			ResultSet lRS = DatabaseUtil.runSelectStatement(DaoHelper.getDBConnection(), lStrQuery);
			ContactTagDAOImpl lDAO = new ContactTagDAOImpl();
			List lList = lDAO.getResults(lRS);
			ListIterator lListIter = lList.listIterator();
			int liSize = lList.size();
			
			lContactTag = new TagForContact[liSize];
			for(int i = 0; i < liSize; i++)
			{
				if(lListIter.hasNext())
				{
					lContactTag[i] = new TagForContact();
					lContactTag[i] = mapDaoToDomain((ContactTag)lListIter.next());
				}
				else
				{
					cLogger.warn("The number of tags is more than what is in the list.");
					break;
				}
			}
			
		}
		catch(Exception ex)
		{
			cLogger.error("Error occured retrieving the contacts with query: " 
					+ lStrQuery, ex);
		}
		
		return lContactTag;
	}
	
	public static ContactTag mapDomainToDao(TagForContact myObject)
	{
		return (ContactTag) myObject;
	}
	
	public static TagForContact mapDaoToDomain(ContactTag myDaoObject)
	{
		TagForContact lDomain = null;
		
		if(myDaoObject != null) {
			lDomain = new TagForContact();
			lDomain.setIdContact(myDaoObject.getIdContact());
			lDomain.setSTag(myDaoObject.getSTag());
		}
		else
		{
			cLogger.warn("Attempted to map a null object.");
		}
		
		return lDomain;
	}
}
